//
//  XNContractViewController.m
//  XINNingOA
//
//  Created by samuel on 2022/2/14.
//

#import "XNContractViewController.h"
#import "XNContractBillInfoRequest.h"
#import "XNContractApproveRequest.h"

@interface XNContractViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNContractBillInfoModel *infoModel;

@end

@implementation XNContractViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNContractBillInfoRequest *infoRequest = [[XNContractBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNContractBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNContractBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNContractApproveRequest *approveRequest = [[XNContractApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNContractApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNContractApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"审核单号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"创建日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"创建人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"合同编号： %@",self.infoModel.conCode],
//                              [NSString stringWithFormat:@"合同名称： %@",self.infoModel.conName],
//                              [NSString stringWithFormat:@"合同类型： %@",self.infoModel.typeName],
                              [NSString stringWithFormat:@"承办人： %@",self.infoModel.chargeManStr],
                              [NSString stringWithFormat:@"承办部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"责任人： %@",self.infoModel.responsibleStr],
                              [NSString stringWithFormat:@"是否续签合同： %@",self.infoModel.oldCode.length > 0 ? @"是" : @"否"],
                              [NSString stringWithFormat:@"原合同号： %@",self.infoModel.oldCode.length > 0 ? self.infoModel.oldCode : @""],
//                              [NSString stringWithFormat:@"我方角色： %@",self.infoModel.partnerTypeName],
                              [NSString stringWithFormat:@"合作客户(供应商)： %@",self.infoModel.partnerName],
                              [NSString stringWithFormat:@"主要内容： %@",self.infoModel.conContent],
                              [NSString stringWithFormat:@"到期时间： %@",self.infoModel.expirationDateStr],
                              [NSString stringWithFormat:@"到期提醒： %ld天",(long)self.infoModel.remindDay],
                              [NSString stringWithFormat:@"备注： %@",self.infoModel.remark],
                              @"附件信息：",
                              nil];
    itemModel.showItemArray = showItemArray;
    itemModel.fileArray = self.infoModel.fileList;
    itemModel.fileIsImage = NO;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
}

@end
